﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Assessment.Management.DAO
{
    class DataAccessHelper
    {
        SqlConnection conn;
        SqlCommand cmd;
        private SqlDataAdapter dataAp;
        private DataTable dataTable;
        private SqlDataReader dataDr;
        string connect = @"Data Source=ColdZero-VAIO\SQLEXPRESS;Initial Catalog=Assessment.Management;Integrated Security=True";
        public DataAccessHelper()
        {
        }

        private void Open()
        {
            conn = new SqlConnection(connect);
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
        }
        private void Close()
        {
            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
        }

        public int CheckExitsData(string query)
        {
            Open();
            cmd = new SqlCommand(query, conn);
            return cmd.ExecuteNonQuery();
        }

        public int ExecuteQuery(string query, string[] pNames, object[] pValues)
        {
            Open();
            cmd = new SqlCommand(query, conn);
            SqlParameter p;
            for (int i = 0; i < pNames.Length; i++)
            {
                p = new SqlParameter(pNames[i], pValues[i]);
                cmd.Parameters.Add(p);
            }
            return cmd.ExecuteNonQuery();
        }
    }
}
